package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.CitiesDomain;
import com.xingkeduo.pojo.json.CitiesJson;
import com.xingkeduo.pojo.json.VendorJson;
import com.xingkeduo.pojo.query.CitiesQuery;
import com.xingkeduo.pojo.query.VendorQuery;
import com.xingkeduo.pojo.table.CitiesTable;
import com.xingkeduo.pojo.table.VendorTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import sun.misc.Request;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 城市管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/cities")
public class CitiesController extends InitController {

    @Autowired
    private CitiesDomain citiesDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            CitiesQuery citiesQuery = CitiesJson.createQueryForCities(urlParam);
            long id = citiesQuery.getId();
            CitiesTable citiesTable = citiesDomain.getCitiesInfoById(id);
            if(CommonUtil.isEmpty(citiesTable)){
                return returnUtil.getException("0","数据为空");
            }
            return returnUtil.getSuccess(CitiesJson.toJson(citiesTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody CitiesTable citiesTable , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            citiesTable.setCreatedBy(this.getOperator(request));
            log.info("添加城市接口:{}", JSON.toJSONString(citiesTable));
            int cities = citiesDomain.createCities(citiesTable);
            log.info("添加城市接口返回值:{}", JSON.toJSONString(cities));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid CitiesTable citiesVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
           // this.errMsg(bindingResult);
            citiesVo.setCreatedBy(this.getOperator(request));
            CitiesTable citiesTable = CitiesJson.createTableForModifyCities(urlParam,citiesVo);
            log.info("修改城市接口:{}", JSON.toJSONString(citiesTable));
            int i = citiesDomain.modifyCities(citiesTable);
            log.info("修改城市接口返回值:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            CitiesQuery citiesQuery = CitiesJson.createQueryForCities(urlParam);
            long id = citiesQuery.getId();
            citiesDomain.delCities(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            CitiesQuery citiesQuery = new CitiesQuery();

            citiesQuery =  JSON.parseObject(urlParam,CitiesQuery.class);
            log.info("查询城市列表接口--urlParam:{}", JSON.toJSONString(citiesQuery));
            List<CitiesTable> citiesList= citiesDomain.getCitiesListByVendorId(citiesQuery);

            return returnUtil.getSuccess(CitiesJson.toJson(citiesList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @GetMapping(value = "/list")
    public ResponseUtils doGetLists(CitiesQuery citiesQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<CitiesTable> citiesList= citiesDomain.getCitiesListByVendorId(citiesQuery);
            return ResponseUtils.getSuccess(citiesList);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }
}
